package com.cardone.platform.common.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dto.EntityFileDto;
import com.cardone.platform.common.service.EntityFileService;

/**
 * 实体文件
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/common/entityFile/")
@Controller
public class EntityFileController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 实体文件：删除
	 *
	 * @param deleteEntityFile
	 *          实体文件对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final EntityFileDto deleteEntityFile) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityFileService.class).deleteByIds(deleteEntityFile);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityFileController.log);
				}
			}
		};
	}

	/**
	 * 实体文件：分页查询
	 *
	 * @param findEntityFile
	 *          实体文件对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final EntityFileDto findEntityFile, final Model model) {
		final EntityFileDto entityFile = ContextHolder.getBean(EntityFileService.class).findById(EntityFileDto.class, findEntityFile);

		model.addAttribute("entityFile", entityFile);
	}

	/**
	 * 实体文件：编辑
	 *
	 * @param findEntityFile
	 *          实体文件对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<EntityFileDto>> findByIdJson(final EntityFileDto findEntityFile, final Model model) {
		return new Callable<ReturnDataDto<EntityFileDto>>() {
			@Override
			public ReturnDataDto<EntityFileDto> call() throws Exception {
				try {
					final EntityFileDto entityFile = ContextHolder.getBean(EntityFileService.class).findById(EntityFileDto.class, findEntityFile);

					return ReturnDataUtils.newReturnData(entityFile);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityFileController.log);
				}
			}
		};
	}

	/**
	 * 实体文件：查询
	 *
	 * @param findListEntityFile
	 *          实体文件对象
	 * @return 实体文件对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<EntityFileDto>>> findListByLikeCodeJson(final EntityFileDto findListEntityFile) {
		return new Callable<ReturnDataDto<List<EntityFileDto>>>() {
			@Override
			public ReturnDataDto<List<EntityFileDto>> call() throws Exception {
				try {
					final List<EntityFileDto> entityFileList = ContextHolder.getBean(EntityFileService.class).findListByLikeCode(EntityFileDto.class, findListEntityFile);

					return ReturnDataUtils.newReturnData(entityFileList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityFileController.log);
				}
			}
		};
	}

	/**
	 * 实体文件：分页查询
	 *
	 * @param paginationEntityFile
	 *          实体文件对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<EntityFileDto>>> paginationByLikeCodeJson(final EntityFileDto paginationEntityFile) {
		return new Callable<ReturnDataDto<PaginationDto<EntityFileDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<EntityFileDto>> call() throws Exception {
				try {
					final PaginationDto<EntityFileDto> entityFilePagination = ContextHolder.getBean(EntityFileService.class).paginationByLikeCode(EntityFileDto.class, paginationEntityFile);

					return ReturnDataUtils.newReturnData(entityFilePagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityFileController.log);
				}
			}
		};
	}

	/**
	 * 实体文件：分页查询
	 *
	 * @param paginationEntityFile
	 *          实体文件对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final EntityFileDto paginationEntityFile, final Model model) {
		final PaginationDto<EntityFileDto> entityFilePagination = ContextHolder.getBean(EntityFileService.class).paginationByLikeCode(EntityFileDto.class, paginationEntityFile);

		model.addAttribute("entityFilePagination", entityFilePagination);
	}

	/**
	 * 实体文件：保存
	 *
	 * @param saveEntityFile
	 *          实体文件对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final EntityFileDto saveEntityFile) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityFileService.class).saveByIdOrCode(EntityFileDto.class, saveEntityFile);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityFileController.log);
				}
			}
		};
	}
}
